473,396 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

problem mutex-thread "Unlocking mutex owned by another thread ???"

Hello, i'm trying to program a thread that would be locked (by a
mutex) and that would only be unlocked once that a function
(generating data) is done. The purpose is to generate data, and unlock
the mutex in order to activate the thread once the data is generated.
I have to do it this way, i can only call the thread if the data are
generated.

************************************************** ******
step 1: initialize the mutex

mx (the mutex is part of the class "cl")

class Client
{
...

public:
pthread_mutex_t mx;

...
}

pthread_mutex_init(&cl->mx, NULL);
************************************************** ******

************************************************** ******
step 2:

as i'm calling a thread, i have to pass in param a structure with the
mutex :

struct S_PARAMS
{
...

pthread_mutex_t *mx;

...
} typedef T_PARAMS;

i create the thread and i copy the mutex in the param structure :

pthread_t thread;
params->mx = &cl->mx;

if ((thId = pthread_create(&thread, NULL, OBody::thFileSender, (void
*)params)) != 0)
{
THREAD NOT CREATED ... throw ...
}
else
{
I SET SOME FLAG TO KNOW MY THREAD HAS BEEN CREATED
}
************************************************** ******

************************************************** ******
step 3:

i lock my thread :

void *OBody::thFileSender(void *lpParam)
{
cout << "locking MUTX*****" << endl;
cout << pthread_mutex_lock(params->mx) << endl;
cout << "unlocked MUTX *****" << endl;

... (sending data)
}

************************************************** ******

************************************************** ******
step 4:

once i know my functions generating data are done, i call the thread
unlocking function.
cout << "*** before unlock ***" << endl;
pthread_mutex_unlock(&cl.mx);
cout << "*** after unlock ***" << endl;

************************************************** ******
Finally i get :

************************************************** ******

....

locking MUTX*****
0
unlocked MUTX *****

.... (generated data)
*** before unlock ***
server: Error detected by libpthread: Unlocking mutex owned by another
thread.
Detected by file "/root/netbsd-3.0/src/lib/libpthread/
pthread_mutex.c", line 347, function "pthread_mutex_unlock".
See pthread(3) for information.

....

************************************************** ******
What i understand is that the mutex is not locking the thread because
in the step3,
the "cout << "locking MUTX*****" << endl;" is done when is start the
program.

I put a sleep(10) in order to be sure that the thread was still
running without calling thread_unlock.

Moreover, when i try to unlock the thread, i get the error "Unlocking
mutex owned by another thread.".

all the mutex functions (init, lock, unlock) return 0 which mean that
the function are running correctly.

i have been working on this problem for hours and hours, i'm not a
specialist of the mutex/thread.

Thank you for your help.

Apr 14 '07 #1
3 5502
On Apr 14, 7:11 pm, "NaeiKinDus" <naeikin...@gmail.comwrote:
Hello, i'm trying to program a thread that would be locked (by a
mutex) and that would only be unlocked once that a function
(generating data) is done. The purpose is to generate data, and unlock
the mutex in order to activate the thread once the data is generated.
I have to do it this way, i can only call the thread if the data are
generated.
I'm not sure I understand that paragraph. However, there are
one or two things that look more than dubious...
if ((thId = pthread_create(&thread, NULL, OBody::thFileSender, (void
*)params)) != 0)
What are you passing as third pointer? It has to be a global
function, with `extern "C"' linkage. If OBody is a namespace,
this could be OK, but the rest of the code makes me think that
OBody is member function of a class, and there is no way that a
member function of a class can be passed as an argument to a
function, period, and there's also no way that it can have "C"
linkage.
i lock my thread :
void *OBody::thFileSender(void *lpParam)
{
cout << "locking MUTX*****" << endl;
cout << pthread_mutex_lock(params->mx) << endl;
cout << "unlocked MUTX *****" << endl;
... (sending data)
}
Where do you unlock it? (Your output says "unlocked MUTX", but
it looks to me like you just locked it.) After this operation,
the thread which has executed the pthread_mutex_lock owns the
mutex. No one else can unlock it, for rather obvious reasons.

In C++, the usual way of handling this is to use a RAII class;
that way, the mutex will be unlocked even if there is an
exception.

[...]
What i understand is that the mutex is not locking the thread because
in the step3,
I'm not sure what you mean by "the mutex locking the thread". A
thread acquires a mutex. Once a thread has acquired a mutex,
then any other thread which attempts to acquire it will block
until the thread holding the mutex releases
(pthread_mutex_unlock) it.

[...]
Moreover, when i try to unlock the thread, i get the error
"Unlocking mutex owned by another thread.".
So you know exactly what your error is.
all the mutex functions (init, lock, unlock) return 0 which mean that
the function are running correctly.
i have been working on this problem for hours and hours, i'm not a
specialist of the mutex/thread.
You might want to check out the Butenhof. It's the standard
reference for Posix threading. (I also recommend it for people
doing Windows programming, as it has enough general information
to be of use to them as well.)

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 14 '07 #2
On 15 avr, 01:43, "James Kanze" <james.ka...@gmail.comwrote:
On Apr 14, 7:11 pm, "NaeiKinDus" <naeikin...@gmail.comwrote:
Hello, i'm trying to program a thread that would be locked (by a
mutex) and that would only be unlocked once that a function
(generating data) is done. The purpose is to generate data, and unlock
the mutex in order to activate the thread once the data is generated.
I have to do it this way, i can only call the thread if the data are
generated.

I'm not sure I understand that paragraph. However, there are
one or two things that look more than dubious...
Rephrased: I had to block a thread until the calling process says it
can do what it's meant to do.
>
if ((thId = pthread_create(&thread, NULL, OBody::thFileSender, (void
*)params)) != 0)

What are you passing as third pointer? It has to be a global
function, with `extern "C"' linkage. If OBody is a namespace,
this could be OK, but the rest of the code makes me think that
OBody is member function of a class, and there is no way that a
member function of a class can be passed as an argument to a
function, period, and there's also no way that it can have "C"
linkage.
Nevermind. Just a noob thing a mate did (we're working in group ) -_-'
By the way, why does the function actually works ? It was successfully
called !
Anyway... Fixed.
i lock my thread :
void *OBody::thFileSender(void *lpParam)
{
cout << "locking MUTX*****" << endl;
cout << pthread_mutex_lock(params->mx) << endl;
cout << "unlocked MUTX *****" << endl;
... (sending data)
}

Where do you unlock it? (Your output says "unlocked MUTX", but
it looks to me like you just locked it.) After this operation,
the thread which has executed the pthread_mutex_lock owns the
mutex. No one else can unlock it, for rather obvious reasons.
The "Unlocked" cout was supposed to appear when the thread would have
been released...

What i understand is that the mutex is not locking the thread because
in the step3,

I'm not sure what you mean by "the mutex locking the thread". A
thread acquires a mutex. Once a thread has acquired a mutex,
then any other thread which attempts to acquire it will block
until the thread holding the mutex releases
(pthread_mutex_unlock) it.

[...]
Got it :) Thanks, we'll work on it !
>
Moreover, when i try to unlock the thread, i get the error
"Unlocking mutex owned by another thread.".

So you know exactly what your error is.
all the mutex functions (init, lock, unlock) return 0 which mean that
the function are running correctly.
i have been working on this problem for hours and hours, i'm not a
specialist of the mutex/thread.

You might want to check out the Butenhof. It's the standard
reference for Posix threading. (I also recommend it for people
doing Windows programming, as it has enough general information
to be of use to them as well.)
Again, thanks for your help !
--
James Kanze (Gabi Software) email: james.ka...@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 15 '07 #3
On Apr 15, 11:28 am, "NaeiKinDus" <naeikin...@gmail.comwrote:
On 15 avr, 01:43, "James Kanze" <james.ka...@gmail.comwrote:
On Apr 14, 7:11 pm, "NaeiKinDus" <naeikin...@gmail.comwrote:
Hello, i'm trying to program a thread that would be locked (by a
mutex) and that would only be unlocked once that a function
(generating data) is done. The purpose is to generate data, and unlock
the mutex in order to activate the thread once the data is generated.
I have to do it this way, i can only call the thread if the data are
generated.
I'm not sure I understand that paragraph. However, there are
one or two things that look more than dubious...
Rephrased: I had to block a thread until the calling process says it
can do what it's meant to do.
You mean you start a thread, just so that it can wait until you
decide it can work. In that case, you need for the calling
thread to lock the mutex before starting the thread, then unlock
it when its OK. The called thread tries acquire the mutex,
blocking until the calling thread releases it. Just don't
forget to have the called thread release the mutex too, when it
is through.

Still, I would definitly consider just putting off starting the
thread until it can do what it's meant to do.
if ((thId = pthread_create(&thread, NULL, OBody::thFileSender, (void
*)params)) != 0)
What are you passing as third pointer? It has to be a global
function, with `extern "C"' linkage. If OBody is a namespace,
this could be OK, but the rest of the code makes me think that
OBody is member function of a class, and there is no way that a
member function of a class can be passed as an argument to a
function, period, and there's also no way that it can have "C"
linkage.
Nevermind. Just a noob thing a mate did (we're working in group ) -_-'
By the way, why does the function actually works ? It was successfully
called !
I don't know. As I said, to begin with, supposing that
thFileSender is a member function, the code shouldn't even
compile; you can't pass a member function as a parameter,
period, and there's no implicit conversion of member function to
pointer. (I think VC++ did implicitly convert a member function
to a pointer without a &.)

The second thing is that the type of the function is wrong. If
the function is not static, of course, the type is way, way
wrong, and I can't imagine it ever working (but again, I've
heard about some wierd behavior in VC++). But even if the
member function is static, it still won't be `extern "C"'. Now,
Posix doesn't define a C++ binding, and C++ doesn't say anything
about pthread_create, so it's up to the implementation. But in
all the implementations I know (Solaris and Linux),
pthread_create is NOT overloaded, and requires a function with
"C" linkage. The compiler should complain. (Sun CC does,
although it is only a warning, and not an error. G++ doesn't;
this is a bug in g++.)
Anyway... Fixed.
i lock my thread :
void *OBody::thFileSender(void *lpParam)
{
cout << "locking MUTX*****" << endl;
cout << pthread_mutex_lock(params->mx) << endl;
cout << "unlocked MUTX *****" << endl;
... (sending data)
}
Where do you unlock it? (Your output says "unlocked MUTX", but
it looks to me like you just locked it.) After this operation,
the thread which has executed the pthread_mutex_lock owns the
mutex. No one else can unlock it, for rather obvious reasons.
The "Unlocked" cout was supposed to appear when the thread would have
been released...
Before going any further, you're going to have to read a general
book about threading, at least enough to know the basic
vocabulary. What do you mean by "released"? A thread is
started, it runs, and it exits (terminates). If it hasn't been
detached, someone, somewhere, must join with it. But it's never
"released".

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Chris W | last post by:
I have a simple html form that sends 3 values to perl cgi script. The perl script then makes some calculations and prints the results to the browser. It also writes the values to a file with some...
0
by: Walt | last post by:
I'm running SQL Server 2000 SP2 transactional replication. Periodically replication fails due to primary key errors. On investigation I find that the offending transaction is attempting to insert...
0
by: Yasin Gedik | last post by:
Hello, I have a problem with autoscroll property. I am designing a form for pocket pc 2003 in whidbey, and deploying it to emulator. I set the forms Autoscroll property true and put...
0
by: nik | last post by:
I have a postback on an aspx-page. In this postback I perform a redirect. The page is quite heavy, so instead of performing a postback I perform a post to another empty page, which then can do the...
1
by: Melissa | last post by:
I have inherited an intranet ASP.NET (C#) page running on Windows 2000. The page populates a label control with some javascript to open a pop-up to a document (xls, doc, pdf, etc.). This is...
2
by: Joe Coppola | last post by:
Hi there. I am having a weird problem. Currently I have a straight ASP page that queries an Access database and returns the large recordset on a page that loops through the recordset and writes...
5
by: John F | last post by:
Hi all, I posted a question earlier but have since discovered something else. If anyone read my previous post, ignore the issue surrounding two different url's to one box since I've managed to...
0
by: xudeutsch | last post by:
I have installed IIS and Microsoft Visual Studio .NET 2003 in window 2000 Server but on the virtual machine of VMWare . When i create a new ASP.NET Web Application project, there is an erro...
1
by: xudeutsch | last post by:
Maybe no one is interested but i need the solution. This time followed the steps made by Microsoft:install IIS, .net and Visual Studi components that are required for Visual Studio to be able t...
2
by: rn5a | last post by:
I am working on WinXP Pro (SP2) & use IIS5.1 as the intranet server to run ASP applications. I connect to the Internet using a LAN connection. My PC is a standalone machine. Of late, what I find...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.