473,854 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

compilation problem - variable was not declared in this scope

Can anyone look at this code and let me know why pthread_mutex_u nlock
and pthread_mutex_l ock are giving me the "phtread_mutex_ unlock" was
not defined in this scope error.
I am compiling the code on g++.
[root@localhost Stargate_GPS]# g++ -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
enable-checking=releas e --with-system-zlib --enable-__cxa_atexit --
disable-libunwind-exceptions --enable-libgcj-multifile --enable-
languages=c,c++ ,objc,obj-c++,java,fortra n,ada --enable-java-awt=gtk --
disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-
gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)

The header file is:
----------------
#ifndef UDP_COMM_H
#define UDP_COMM_H

// General Requirements
#include <iostream>

// UDP requirements
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
// Serial requirements
#include <stdio.h // Standard input/output definitions
#include <string // String function definitions
#include <unistd.h // UNIX standard function definitions
#include <fcntl.h // File control definitions
#include <termios.h// POSIX terminal control definitions

#define MAXCOMMBUF 1024
#define MAXWAIT 100

#define GPS_PORT "/dev/tts/0"
#define ROBO_PORT "/dev/tts/2"
#define ROBO_COMM_SIZE 13

// Communications data structure definition
struct commdata {
char* address;
char* port;
pthread_mutex_t inlock, outlock, contlock; // mutex
int receive;
int send;
int stop;
int insize, outsize;
char inbuff[MAXCOMMBUF];
char outbuff[MAXCOMMBUF];
int fd; //file descriptor
};

using namespace std;

void* commThread(void * arg); // wrapper function for thread function
call
int open_port(commd ata* data, int speed);
int open_socket(com mdata* data, struct sockaddr_in &read_addr,stru ct
sockaddr_in &write_addr) ;

#endif

----------------

The source is:
-------------------
#include "comm.h"

void* commThread(void * arg)
{
// Comm control variables
int count = 0;
int nb, nbytes, outsize;
fd_set read_template;
struct timeval wait;

// Comm thread buffers
char readbuff[MAXCOMMBUF];
char writebuff[MAXCOMMBUF];
for (int i=0;i<MAXCOMMBU F;i++) {
readbuff[i] = 0;
writebuff[i] = 0;
}
// Thread input pointer
commdata** data = (commdata**) arg;

////////////////////////////////////
// UDP socket setup
////////////////////////////////////
int sockfd;
struct sockaddr_in read_addr, write_addr;
sockfd = open_socket(dat a[0], read_addr, write_addr);
if (sockfd < 0) return(NULL);
cout << "UDP socket now opened" << endl;

////////////////////////////////////
// serial port setup
////////////////////////////////////
int curport[2];

// opening port with fixed options
curport[0] = open_port(data[1], B19200);
if (curport[0] < 0) return(NULL);
curport[1] = open_port(data[2], B115200);
if (curport[1] < 0) return(NULL);
cout << "Serial ports now opened" << endl;

////////////////////////////////////
// Main Loop
////////////////////////////////////
while(1) {

// Check if thread should quit
pthread_mutex_l ock(&(data[2]->contlock));
if (data[2]->stop) {
cout << "Serial thread received exit command" << endl;
pthread_mutex_u nlock(&(data[2]->contlock));
break;
}
pthread_mutex_u nlock(&(data[2]->contlock));

// Check for available packets with timeout
// set timeout
wait.tv_sec = 0;
wait.tv_usec = MAXWAIT;
// define where to read from
FD_ZERO(&read_t emplate);
FD_SET(sockfd, &read_template) ;
FD_SET(curport[0], &read_template) ;
FD_SET(curport[1], &read_template) ;

// Check udp and both serial ports to see if data is available
if ((nb = select(FD_SETSI ZE, &read_templa te, NULL,NULL, &wait)) >
0) {
//cout << "Data available:" << FD_ISSET(curpor t[0],
&read_templa te) << " " << FD_ISSET(curpor t[1], &read_templa te) << " "
<< FD_ISSET(sockfd , &read_templa te) << endl;
// Read serial data if available
for (int i=0;i<2;i++) {
if (FD_ISSET(curpo rt[i], &read_template) ) {
nbytes = read(curport[i], readbuff, MAXCOMMBUF);
//cout << "Serial " << i+1 << ": " << nbytes << endl;
if (nbytes < 0) {
cout << " Read error" << endl;
continue;
} else if (nbytes 0) {
pthread_mutex_l ock(&(data[i+1]->inlock));
for (int j=0;j<nbytes;j+ +) {
// Check inbuff overflow
if (data[i+1]->insize+j >= MAXCOMMBUF) {
data[i+1]->insize = 0;
cout << "Serial thread inbuff overflow" << endl;
}
// Copy readbuff to inbuff
data[i+1]->inbuff[data[i+1]->insize+j] = readbuff[j];
readbuff[j] = 0;
}
data[i+1]->insize += nbytes;
data[i+1]->receive = 1;
pthread_mutex_u nlock(&(data[i+1]->inlock));
}
}
}

// Read UDP data if available
if (FD_ISSET(sockf d, &read_template) ) {
nbytes = read(sockfd, readbuff, MAXCOMMBUF);
if (nbytes < 0) {
cout << " Read error" << endl;
continue;
}
// save received data
pthread_mutex_l ock(&(data[0]->inlock));
for (int i=0;i<nbytes;i+ +) {
if (data[0]->insize+i >= MAXCOMMBUF) {
data[0]->insize = 0;
cout << "UDP thread inbuff overflow" << endl;
}
data[0]->inbuff[data[0]->insize + i] = readbuff[i];
}
count++;
data[0]->insize += nbytes;
data[0]->receive = 1;
pthread_mutex_u nlock(&(data[0]->inlock));
}
}

// Check if udp packet is ready to send
pthread_mutex_l ock(&(data[0]->outlock));
if (!(data[0]->send)) {
pthread_mutex_u nlock(&(data[0]->outlock));
} else {
// grab data to transmit
for (int i=0;i<data[0]->outsize;i++) {
writebuff[i] = data[0]->outbuff[i];
}
// reset transmit flag
outsize = data[0]->outsize;
data[0]->outsize = 0;
data[0]->send = 0;
pthread_mutex_u nlock(&(data[0]->outlock));
// send individual udp packets
if (sendto(sockfd, writebuff,sizeo f(char)*outsize ,0,(struct sockaddr
*)&write_addr,s izeof(write_add r)) < 0) {
cout << "Error Transmitting data packet to ground" << endl;
}
// send udp packet to screen
// cout << "UDP Out Packet: " ;
// for (int j=0;j<outsize;j ++) {
// cout << (int) writebuff[j] << " ";
// }
// cout << endl;
// cout << "UDP Output size: " << outsize << endl;
}

// Check if serial packet is ready to send
for (int i=0;i<2;i++) {
pthread_mutex_l ock(&(data[i+1]->outlock));
if (!(data[i+1]->send)) {
pthread_mutex_u nlock(&(data[i+1]->outlock));
} else {
// grab data to transmit
for (int j=0;j<data[i+1]->outsize;j++) {
writebuff[j] = data[i+1]->outbuff[j];
}
outsize = data[i+1]->outsize;

// reset transmit flag
data[i+1]->outsize = 0;
data[i+1]->send = 0;
pthread_mutex_u nlock(&(data[i+1]->outlock));
// send serial packet
nb = write(curport[i], writebuff, outsize*sizeof( char));

// if (i == 1) {
// cout << "Serial out: " ;
// for (int j=0;j<outsize;j ++) {
// cout << (int) writebuff[j] << " ";
// if (j>0) {
// if ((writebuff[j-1] ==16) && (writebuff[j]==3)) cout <<
endl;
// }
// }
// cout << endl;
// }
}
}
}
// Close comm items
close(sockfd);
cout << "UDP socket closed, thread terminated" << endl;
for (int i=0;i<2;i++) {
close(curport[i]);
}
cout << "Serial ports closed, thread terminated" << endl;
return(NULL);
}
// 'open_port()' - Open serial port.
// Returns the file descriptor on success or -1 on error.
int open_port(commd ata* data, int speed)
{
int fd; // File descriptor for the port
struct termios options;

fd = open(data->port, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
//Could not open the port.
perror("open_po rt: Unable to open serial port - ");
}
else {
// Read currently available data without blocking;
//fcntl(fd, F_SETFL, FNDELAY);
// Read currently available data with blocking;
fcntl(fd, F_SETFL, 0);
}

// Get default attributes
tcgetattr(fd, &options);

// Clear all default attributes
bzero(&options, sizeof(options) );

// Enable the receiver and set local mode...
options.c_cflag = CS8 | CLOCAL | CREAD;

// Ensure no dumb options are on. Ignore breaks or bytes with parity
errors
options.c_iflag = IGNBRK|IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0; //~(ICANON | ECHO | ECHOE | ISIG); // Set the
new options for the port...

// Set the baud rates to speed
cfsetispeed(&op tions, speed);
cfsetospeed(&op tions, speed);

/*// Set to 8N1
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// Disable flow control
options.c_cflag &= ~CRTSCTS;
options.c_iflag &= ~ICRNL;
options.c_iflag &= ~(IXON | IXOFF | IXANY);

// Enable the receiver and set local mode...
options.c_cflag |= (CLOCAL | CREAD);

// The magical line, turn the port to non-canonical mode so it just
spits data out.
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

// Setup output options
options.c_oflag = 0;
*/
// Set the new options for the port...
tcsetattr(fd, TCSANOW, &options);
return (fd);
}

int open_socket(com mdata* data, struct sockaddr_in &read_addr,stru ct
sockaddr_in &write_addr)
{
int sockfd;
struct hostent *hp;

// Open a UDP socket (an Internet datagram socket).
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
cout << "Unable to open UDP socket" << endl;
return (sockfd);
}

// Setup local address
bzero((char *) &read_addr, sizeof(read_add r));
read_addr.sin_f amily = AF_INET;
read_addr.sin_a ddr.s_addr = INADDR_ANY;
read_addr.sin_p ort = htons(atoi(data->port));
if (bind (sockfd, (struct sockaddr *)&read_addr, sizeof(read_add r))
< 0) {
cout << "Error binding to port " << data->port << endl;
return (sockfd);
}

// Setup remote address
if ((hp = gethostbyname(d ata->address)) == 0){
cout << "Unable to get host by name" << endl;
return (sockfd);
}
bzero((char *) &write_addr, sizeof(write_ad dr));
write_addr.sin_ family = AF_INET;
bcopy((char *)hp->h_addr, (char *)&write_addr.s in_addr, hp-
>h_length);
write_addr.sin_ port = htons(atoi(data->port));
return (sockfd);
}

-------------------

The following are the errors:
comm.c: In function 'void* commThread(void *)':
comm.c:50: error: 'pthread_mutex_ lock' was not declared in this scope
comm.c:53: error: '::pthread_mute x_unlock' has not been declared
comm.c:56: error: 'pthread_mutex_ unlock' was not declared in this
scope

Thanks

Jul 5 '07 #1
3 16573
ps****@gmail.co m wrote:
Can anyone look at this code and let me know why pthread_mutex_u nlock
and pthread_mutex_l ock are giving me the "phtread_mutex_ unlock" was
not defined in this scope error.
Since you didn't declare them, it probably means that none of your headers
did either. Have you included their header? Else, you have no luck here,
try asking in a group for your operating system.

--
rbh
Jul 5 '07 #2
On 2007-07-05 17:04, ps****@gmail.co m wrote:
Can anyone look at this code and let me know why pthread_mutex_u nlock
and pthread_mutex_l ock are giving me the "phtread_mutex_ unlock" was
not defined in this scope error.
I am compiling the code on g++.
[root@localhost Stargate_GPS]# g++ -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --
infodir=/usr/share/info --enable-shared --enable-threads=posix --
enable-checking=releas e --with-system-zlib --enable-__cxa_atexit --
disable-libunwind-exceptions --enable-libgcj-multifile --enable-
languages=c,c++ ,objc,obj-c++,java,fortra n,ada --enable-java-awt=gtk --
disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-
gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)

The header file is:
----------------
#ifndef UDP_COMM_H
#define UDP_COMM_H

// General Requirements
#include <iostream>

// UDP requirements
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
// Serial requirements
#include <stdio.h // Standard input/output definitions
#include <string // String function definitions
#include <unistd.h // UNIX standard function definitions
#include <fcntl.h // File control definitions
#include <termios.h// POSIX terminal control definitions
#include <pthread.h>

--
Erik Wikström
Jul 5 '07 #3
On Jul 5, 12:28 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-07-05 17:04, psr...@gmail.co m wrote:
Can anyone look at this code and let me know why pthread_mutex_u nlock
and pthread_mutex_l ock are giving me the "phtread_mutex_ unlock" was
not defined in this scope error.
I am compiling the code on g++.
[root@localhost Stargate_GPS]# g++ -v
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man--
infodir=/usr/share/info --enable-shared --enable-threads=posix --
enable-checking=releas e --with-system-zlib --enable-__cxa_atexit --
disable-libunwind-exceptions --enable-libgcj-multifile --enable-
languages=c,c++ ,objc,obj-c++,java,fortra n,ada --enable-java-awt=gtk--
disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.4.2-
gcj-1.4.2.0/jre --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)
The header file is:
----------------
#ifndef UDP_COMM_H
#define UDP_COMM_H
// General Requirements
#include <iostream>
// UDP requirements
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
// Serial requirements
#include <stdio.h // Standard input/output definitions
#include <string // String function definitions
#include <unistd.h // UNIX standard function definitions
#include <fcntl.h // File control definitions
#include <termios.h// POSIX terminal control definitions

#include <pthread.h>

--
Erik Wikström
Thanks Erik, That solved the problem.

Jul 5 '07 #4

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

Similar topics

5
27996
by: William | last post by:
In Peer.h, I have: class Peer { // ... }; In Overseer.h, I have: #include "Peer.h" #include <vector>
3
15734
by: Vaxius | last post by:
After trying to compile the following code, gcc gives me error: 'setwidth' was not declared in this scope I'm not sure why it would do this, since iomanip is included. Am I missing something? code: #include <iostream>
14
9067
by: Darren L. Weber | last post by:
I am trying to compile a utility to create .avi files. See http://cpbotha.net/im2avi I'm working on Debian etch (a mix of testing/unstable). dweber@dnlweber:~/im2avi-0.4$ g++ --version g++ (GCC) 4.0.4 20060507 (prerelease) (Debian 4.0.3-3) Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
4
2351
by: Krice | last post by:
I get the following error: editarea.cpp: In member function `int Edit_Area::Mouse_Draw(int, int, unsigned char)': `g_undo' was not declared in this scope The problem is that g_undo is there. It's in undo.h: class S_Undo
7
5822
by: Frank | last post by:
Hi, I have the following problem with dynamic memory: int main(){ for(){ int (**w)=new int *; for(m = 0; m < N1; m++) {
2
6353
by: Tim H | last post by:
help?!? #include <boost/shared_ptr.hpp> class pp_dirent {}; class pp_scope: public pp_dirent {}; int main() { boost::shared_ptr<pp_direntp1(new pp_scope()); boost::shared_ptr<pp_scopep2 = static_pointer_cast<pp_scope>(p1);
2
1994
by: jr.freester | last post by:
I have a written a simple class called Matrix. It allows me to operate on a 2D array if it were a matrix. I have not really written a class in c++ since the single course I took in college. In the class Matrix I have a function toMatrix that allows me to copy a standard array to a Matrix object. I have written a driver program to test out the class. When compiling with gcc 4.1.2 I get the following error ' error: ‘toMatrix’ was not...
5
2362
by: sfuo | last post by:
Hey guys, I went from working with SDL to OpenGL now and have transferred some code from one project to another. I do not understand why I am getting an error with this: "lib.h" #ifndef LIB_H #define LIB_H #include <windows.h>
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9750
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11024
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9510
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7077
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5738
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5937
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4150
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3182
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.