Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: boost mutex and lock_guard


This question is answered.


Permlink Replies: 3 - Last Post: Jun 6, 2016 1:10 PM Last Post By: James McKenna
James McKenna

Posts: 8
Registered: 5/20/16
boost mutex and lock_guard  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 5, 2016 1:52 PM
Im using C++ builder 10.1 berlin. I installed the boost support via GitIt package manager, and I'm trying to use boost::mutex with boost::unique_lock. The code looks something like this:

#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>

class TLaserComForm : public TForm
{
...
private:
void __fastcall SendCommand(char *buf, int timeout);
...
public: // User declarations
boost::mutex m_SerialBufferMutex; // protects serial buffer access
...
};

void __fastcall TLaserComForm::SendCommand(char *buf, int timeout)
{
boost::lock_guard<boost::mutex> lock(m_SerialBufferMutex); // get exclusive access to m_SerialBuffer
...
}

The code compiles and links fine, but terminates immediately on startup. There does not appear to be an error or exception in the debugger, it just immediately exits though I can't tell exactly where. I've also tried using unique_lock instead of lock_guard with the same behavior. In both cases, commenting out the one line of code allows the program to run. Strangely, the lock_guard is not even actually being called. I've seen in some searches that only some parts of boost are implemented in C++ builder. Is that what is going on here, or any other ideas? I strongly prefer boost mutexes over windows calls or other non-portable methods. TIA.

James McKenna

Posts: 8
Registered: 5/20/16
Re: boost mutex and lock_guard  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 5, 2016 2:17 PM   in response to: James McKenna in response to: James McKenna
Same problem with a brand new VCL project, with only a very few lines added.

Here is Unit1.h:

//---------------------------------------------------------------------------
 
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
 
#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>
 
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
private:	// User declarations
public:		// User declarations
	__fastcall TForm1(TComponent* Owner);
 
	boost::mutex	m_Mutex; // protects serial buffer access
 
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


and here is Unit1.cpp:

//---------------------------------------------------------------------------
 
#include <vcl.h>
#pragma hdrstop
 
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
	if (false)
	{
	   boost::lock_guard<boost::mutex> lock(m_Mutex);
	}
 }
//---------------------------------------------------------------------------
Antonio Estevez

Posts: 665
Registered: 4/12/00
Re: boost mutex and lock_guard
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 5, 2016 2:48 PM   in response to: James McKenna in response to: James McKenna
El 05/06/2016 a las 22:52, James McKenna escribió:
Im using C++ builder 10.1 berlin. I installed the boost support via GitIt package manager, and I'm trying to use boost::mutex with boost::unique_lock. The code looks something like this:

#include <boost/thread/mutex.hpp>
#include <boost/thread/thread.hpp>

Run the application exe from outside of the IDE and you will see a message saying a missing DLL.

You must copy the DLL from the $(BDS)\redist\boost\$(Platform) folder to the same folder where the exe is or to a folder
included in the PATH variable.

Run the exe again to verify if there is another missing DLL

James McKenna

Posts: 8
Registered: 5/20/16
Re: boost mutex and lock_guard  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 6, 2016 1:10 PM   in response to: Antonio Estevez in response to: Antonio Estevez
That was it. Thanks Antonio!
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02