472,992 Members | 3,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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 3895

<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
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
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
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
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
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
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
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
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
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
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
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.