C++ C++;程序在Windows和Linux下编译。但是';我不能在Linux上工作
我认为这可能与我的指针在结构中的使用/初始化方式有关,但我不能完全确定。我使用'g++-lpthreadmain.cpp'进行编译。该程序在Linux中挂起,同时在windows中正确执行。为了调试的目的,我在代码的开头放了一个cout,程序甚至没有吐出来C++ C++;程序在Windows和Linux下编译。但是';我不能在Linux上工作,c++,pointers,struct,initialization,posix,C++,Pointers,Struct,Initialization,Posix,我认为这可能与我的指针在结构中的使用/初始化方式有关,但我不能完全确定。我使用'g++-lpthreadmain.cpp'进行编译。该程序在Linux中挂起,同时在windows中正确执行。为了调试的目的,我在代码的开头放了一个cout,程序甚至没有吐出来 #include "pthread.h" #include "semaphore.h" #include "time_functions.h" #include <iostream> #include <fstream>
#include "pthread.h"
#include "semaphore.h"
#include "time_functions.h"
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
struct vars {
char buffer[10][1000];
int put;
int take;
sem_t openSlot;
sem_t slotInactive;
sem_t newData;
ifstream readFile;
ofstream writeFile;
};
void *write(void *in) //consumer, writes data to file and deletes data from buffer
{
vars* writeVars = (vars*)in;
while (true)
{
sem_wait(&(*writeVars).newData);
sem_wait(&(*writeVars).slotInactive);
if ((*writeVars).buffer[(*writeVars).take % 10][0] != '$')
{
(*writeVars).writeFile << (*writeVars).buffer[(*writeVars).take % 10];
if ((*writeVars).readFile.eof() != true)
{
(*writeVars).writeFile << endl;
}
else
{
break;
}
}
(*writeVars).take++;
sem_post(&(*writeVars).openSlot);
sem_post(&(*writeVars).slotInactive);
}
pthread_exit(0);
return 0;
}
void *read(void *in) //producer, reads data into buffer
{
vars* readVars = (vars*)in;
char read_line[1000];
while ((*readVars).readFile.getline(read_line, 1000))
{
sem_wait(&(*readVars).openSlot);
sem_wait(&(*readVars).slotInactive);
strcpy((*readVars).buffer[(*readVars).put % 10], read_line);
(*readVars).put++;
sem_post(&(*readVars).slotInactive);
sem_post(&(*readVars).newData);
}
sem_wait(&(*readVars).openSlot);
sem_wait(&(*readVars).slotInactive);
(*readVars).buffer[(*readVars).put % 10][0] = '$';
sem_post(&(*readVars).slotInactive);
sem_post(&(*readVars).newData);
pthread_exit(0);
return 0;
}
int main(int argc, char* argv[])
{
char pause[10];
vars *varsPointer, var;
varsPointer = &var;
var.take = 0;
var.put = 0;
var.writeFile.open(argv[2], ios::out);
var.readFile.open(argv[1], ios::in);
start_timing();
sem_init(&var.openSlot, 0, 10);
sem_init(&var.slotInactive, 0, 1);
sem_init(&var.newData, 0, 0);
pthread_t read_Thread, write_Thread;
pthread_create(&read_Thread, NULL, read, varsPointer);
pthread_create(&write_Thread, NULL, write, varsPointer);
pthread_join(read_Thread, NULL);
pthread_join(write_Thread, NULL);
sem_destroy(&var.openSlot);
sem_destroy(&var.slotInactive);
sem_destroy(&var.newData);
stop_timing();
var.readFile.close();
var.writeFile.close();
//Display timer
cout << "wall clock time (ms):" << get_wall_clock_diff() * 1000 << '\n';
cout << "cpu time (ms):" << get_CPU_time_diff() * 1000 << '\n';
cout << "Type Something and Press Enter To Continue";
cin >> pause; //Just used to keep cmd promt open in Windows after program execution
return 0;
}
#包括“pthread.h”
#包括“semaphore.h”
#包括“time_functions.h”
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
结构变量{
字符缓冲区[10][1000];
int put;
int take;
sem_t开口槽;
运动迟缓;
新数据;
ifstream读取文件;
流写入文件的类型;
};
void*write(void*in)//使用者,将数据写入文件并从缓冲区中删除数据
{
vars*writeVars=(vars*)in;
while(true)
{
sem_wait(&(*writeVars).newData);
sem_wait(&(*writeVars).slotInactive);
if((*writeVars.buffer[(*writeVars.take%10][0]!=“$”)
{
(*书面文件)“为什么写文件返回”<代码>空白> */COD>?@ PaOLM返回空* *,因为这是pTrand的期望。我删除了返回0;在两个VoUL*函数中,编译好了,但仍然是相同的问题。不执行。用C++的所有警告和调试信息编译,例如用<代码> G++-STD= C++ 11 - Walth-WOTU-G<代码>,然后使用调试器。(gdb
)你真的应该使用C++ 11个东西,比如lambdas和容器。你需要返回代码< >空> */Cube >?@ PaOLM返回空洞*。因为这就是pTrk所期望的。我删除了返回0;在Vult*函数中,编译好的,但仍然是相同的问题。不执行。用C++的所有警告和调试信息编译,例如g++-std=c++11-Wall-Wextra-g
然后使用调试器(gdb
)。你真的应该使用c++11东西,比如lambdas和容器。你需要cout