473,499 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I implement this with boost::thread?

Hello,

I would like to re-implement this function that currently uses
pthreads:

pthread_mutex_t mutex;
int i = pthread_mutex_init(&mutex, 0);

void MutexCB(int lock)
{
if (lock)
pthread_mutex_lock(&mutex);
else
pthread_mutex_unlock(&mutex);
}

using the boost library.

So the function has to lock the mutex if 'lock' == true, unlock it
otherwise. It has to be thread safe as well.

I had difficulties since the scoped_lock is NOT thread safe, and the
access to the mutex's do_lock()/do_unlock() methods is private.

Regards,
Petru
P.S. Here is a solution I found. It uses:
- recursive mutex,
- a static lock that gives the access to the lock/unlock mechanism (no
d-tor call during the operations) and
- another lock (on the stack) that makes the static lock thread safe.

boost::recursive_mutex m;

void MutexCB(int lock)
{
boost::recursive_mutex::scoped_lock l(m); // it makes the 's'
lock thread safe
static boost::recursive_mutex::scoped_lock s(m, false /*lock
later only if necessary*/);
if (lock)
s.lock();
if (!lock)
s.unlock();
}

As you can see it is more complicated that the pthread solution. is
there possible to write a solution at least as simple as the pthreads
one?

Jun 15 '07 #1
5 3919

<pe*************@gmail.comwrote in message...
Hello,
I would like to re-implement this function that currently uses
pthreads:
[snip]
using the boost library.

So the function has to lock the mutex if 'lock' == true, unlock it
otherwise. It has to be thread safe as well.
I had difficulties since the scoped_lock is NOT thread safe, and the
access to the mutex's do_lock()/do_unlock() methods is private.
Regards, Petru
'Boost library' and 'pthread' are off-topic in this NG (at this point in
time.).
Do you have a standard C++ question?
Also, see this FAQ for guidelines on posting:

http://www.parashift.com/c++-faq-lite/how-to-post.html

--
Bob R
POVrookie
Jun 15 '07 #2
On Jun 16, 1:39 am, "BobR" <removeBadB...@worldnet.att.netwrote:

[...]
'Boost library' and 'pthread' are off-topic in this NG (at
this point in time.).
Pthreads, maybe, but Boost, certainly not. Boost is certainly
C++, and it is not platform specific, so it is on topic.
Do you have a standard C++ question?
And what was the topic of discussion here before 1998? Or has
the charter been radically changed since then?

--
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

Jun 16 '07 #3

James Kanze wrote in message ...
On Jun 16, 1:39 am, "BobR" wrote:

/* """ quote
[...]
'Boost library' and 'pthread' are off-topic in this NG (at
this point in time.).
Pthreads, maybe, but Boost, certainly not. Boost is certainly
C++, and it is not platform specific, so it is on topic.

""" */ unquote

COOL, then wxWidgets CAN be discussed here. <G>

..... wait... <putting on flame suit... OK, Let 'er fly.

To OP: Ooops, sorry.
--
Bob R
POVrookie
Jun 16 '07 #4
On Jun 16, 6:27 pm, "BobR" <removeBadB...@worldnet.att.netwrote:
James Kanze wrote in message ...
On Jun 16, 1:39 am, "BobR" wrote:
/* """ quote
[...]
'Boost library' and 'pthread' are off-topic in this NG (at
this point in time.).
Pthreads, maybe, but Boost, certainly not. Boost is certainly
C++, and it is not platform specific, so it is on topic.
""" */ unquote

COOL, then wxWidgets CAN be discussed here. <G>
Technically, I think so.

Boost, at any rate, is certainly admissible, since it is more or
less and antechambre for standardization, and it is (like the
entire language) very low level.

I think that the actual charter also would accept things like
wxWidgets. It's hard to say whether that was the intent,
however, since such libraries didn't exist at the time.
Existing portable class libraries, such as the Booch components
or USL, were certainly considered acceptable.

Arguably, even MFC is acceptable, because there was, at one
time, and implementation of it for Unix. Practically, I'd argue
that the intent never really included commercial libraries,
however.

At any rate, I'd certainly accept Boost, and pthreads or Windows
threads would be acceptable in the framework of a more general
discussion on thread safety issues (e.g. as an example).
Details discussion of the more subtle aspects of their
interface, of course, is better handled in other forums; Boost
has their own list, and there are dedicated groups for pthreads
and Windows. In the case of Boost, however, I'd say that the
main reason is because that's where the poster is more likely to
get a knowledgeable response, not because it's strictly off
topic here.

--
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

Jun 16 '07 #5
This is the answer I've got on the boost threads group:
http://lists.boost.org/threads-devel/2007/06/0202.php

Regards,
Petru

Jun 27 '07 #6

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

Similar topics

3
2801
by: dingounan | last post by:
how to compile boost.thread,and set it? I have trouble. platform: windows XP compiler: Borland C++ Compiler 5.5.1 commandline tools thanks.
1
7712
by: deluded.soul | last post by:
Hi everyone, I am trying to use the boost multithreading library. I am having a problem as the join() function for the thread never returns. I am using a boolean variable to indicate when the...
0
285
by: fiefie.niles | last post by:
I am having problem with thread. I have a Session class with public string variable (called Message) that I set from my Main program. In the session class it checks for the value of Message while...
5
2371
by: linyanhung | last post by:
I used a boost multi thread in VS 2005 on a Duo Core PC, and made a two thread process. The code is something like this: #include <boost/thread/thread.hpp> void fun1() { //do something
3
7247
by: Gary Wessle | last post by:
#include <boost/thread/thread.hpp> #include <iostream> using namespace std; class waiter { public: waiter(); void waiting(); void preform();
4
42601
by: Gary Wessle | last post by:
Hi given the Boost thread example here http://www-eleves-isia.cma.fr/documentation/BoostDoc/boost_1_29_0/libs/thread/example/thread.cpp the code below attempts to run the example thread while...
0
1012
by: =?Utf-8?B?TkFUSEFMWV9FQw==?= | last post by:
hello everybody, please i need your help, I try to work with thread in c++ , in easiest application work Ok but in application with thread the class doesn't ....the application show an critical...
1
2517
by: Miroslaw Makowiecki | last post by:
Is it boost.thread library have a ability by next of its I is allowed to find out what it is running? How do it? Thanks in advice.
2
2319
by: Chameleon | last post by:
Why this strange output? Why so many d'tor calls? The code: ---------------------------------------------------------- #include <cstdio> #include <boost/thread/thread.hpp> class A {
1
1918
by: Boogie | last post by:
Hi, A specific problem arises when I try to use boost::thread 1.35.0 with Borland Turbo 2006. Code below compiles but when run it throws (in commented line - thread creation): "assertion...
0
7174
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
7220
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7388
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5470
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4919
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4600
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3091
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1427
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
297
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.