Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with locking a mutex in spawned thread

Status
Not open for further replies.

sph147

Programmer
May 17, 2004
19
US
The program I'm writing is pulling packets from a network connection. Then i'm using a simple ring buffer and threads to pass the info to a buffer and pull them off and do work. The ring class (which is templated) has a member mutex. When get get function (which is taking from the ring and is called when i spawn the thread) attempts to lock the mutex i'm getting this error:

error C2664: 'pthread_mutex_lock' : cannot convert parameter 1 from 'struct pthread_mutex_t_ *ringbuffer_T<unsigned char>::* ' to 'struct pthread_mutex_t_ ** '
There is no context in which this conversion is possible

When the put function was only running, the mutex lock was working fine. But now that I spawn a thread w/ pthread_create and and the starting function (the get function) is called is when i'm getting the error.

Any possible suggestions or help would be greatly appreciated, if i'm not clear about anything or you need more information just let me know. Thanks a lot.
 
Hard to say without seeing some code.

On the face of it, it seems like you tried to cast a pointer to the class into a pointer to the mutex.

Implement an access function which returns the mutex within the class.


--
 
Well I may have figured out part of the problem. The function accessing the mutex was static, while the mutex itself was not. If I make the member function non-static, it presents a new problem. When I attempt to call it during pthread_create, it says that paramater is wrong.
this is my call to spawn the thread...
pthread_create(&id, NULL, buffer.getitem, NULL);

In the definition of the class that buffer is, here is the definition of getitem
void *getitem(void *arg)

the error is this...
error C2664: 'pthread_create' : cannot convert parameter 3 from 'void *(void *)' to 'void *(__cdecl *)(void *)'
None of the functions with this name in scope match the target type

is there anyway to call a start function while spawning a thread that is not static so I don't run into either problem?? Thanks again.
 
Well I tried a new solution, made everything in my ring class static. So now it compiles no problem. But when I try to run it i get a linking error for each static variable in the class, for example....

error LNK2001: unresolved external symbol "public: static struct pthread_cond_t_ * ringbuffer_T::cond" (?cond@ringbuffer_T@@2PAUpthread_cond_t_@@A)

I'm pretty sure I have all the include files correct, and think it is because everything is static. I don't know much about static... please help before I lose my hair
 
I'm afraid you've gone past my limited C++ knowledge :-(

--
 
Are you initializing the static member variables in your .cpp file? That's something you have to do with static member variables. Here's an example:

Header file:
Code:
class CTest
{
public:
   CTest();

   static std::string  m_String;
   static int  m_Num;
};

.cpp file:
Code:
std::string  CTest::m_String;  // Initialize to empty string.
int  CTest::m_Num = 0;  // Initialize to 0.

Note that you also include the variable type in the .cpp file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top